package com.study.spmongodb.book.wwj.char07;

import java.util.concurrent.TimeUnit;

/**
 * @author MI
 * @version 1.0
 * @date 2020/12/1 11:33
 */
public class ThreadHook {
    public static void main(String[] args) {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                    System.out.println("the hook thread 1 is running .");
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("the hook thread 1 is stopping .");
                })
        );

        //钩子程序可以注册多个
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                    System.out.println("the hook thread 2 is running .");
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("the hook thread 2 is stopping .");
                })
        );
        System.out.println("the program will is stopping.");
    }
}
